<template>
  <div style="margin-bottom: -49px">
    <van-nav-bar
      v-if="film"
      @click-left="handleLeft"
      @click-right="handleRight"
      style="position: sticky; top: 0; z-index: 2"
    >
      <template #title>
        <span class="left-text">{{ film.name }}</span>
      </template>
      <template #left>
        <van-icon name="arrow-left" color="#949595" size="20" />
      </template>
      <template #right>
        <van-icon name="search" size="24" color="#000" />
      </template>
    </van-nav-bar>
    <van-tabs
      animated
      :ellipsis="false"
      @click="changeIndex"
      color="#FF6C29"
      title-active-color="#FF6C29"
      line-width="80px"
      line-height="2px"
    >
      <van-tab v-for="item in dateFormat" :key="item">
        <template #title>{{ item | datefilter }}</template>
        <template #default>
          <!-- <div class="wrapper" :style="{height: calcHeight}"> -->
          <ul>
            <li
              v-for="cinema in dateCinames"
              :key="cinema.cinemaId"
              @click="
                handleFilm(
                  cinema.cinemaId,
                  $route.params.filmId,
                  item.timeStamp
                )
              "
            >
              <div class="col-1">
                <p>{{ cinema.name }}</p>
                <span>{{ cinema.address }}</span>
              </div>
              <div class="col-2">
                <div>
                  ￥<span>{{ cinema.lowPrice / 100 }}</span
                  >起
                </div>
                <p>距离未知</p>
              </div>
            </li>
          </ul>
          <!-- </div> -->
        </template>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import http from '@/utils/http'
import obj from '@/utils/mixin'
// import BetterScroll from 'better-scroll'
import { mapState, mapActions } from 'vuex'
import Vue from 'vue'
import moment from 'moment'

moment.locale('zh-cn') // 设置成中文

Vue.filter('datefilter', (date) => {
  const today = moment().startOf('day').unix('x') // 今天
  const tomorrow = moment().add(1, 'days').startOf('days').unix('x') // 明天
  const afterTomorrow = moment().add(2, 'days').startOf('days').unix('x') // 后天

  // console.log(today, tomorrow, afterTomorrow);
  if (date === today) {
    return moment(date * 1000).format('今天MMMDo')
  } else if (date === tomorrow) {
    return moment(date * 1000).format('明天MMMDo')
  } else if (date === afterTomorrow) {
    return moment(date * 1000).format('后天MMMDo')
  } else {
    return moment(date * 1000).format('dddMMMDo')
  }
})

export default {
  mixins: [obj],
  data () {
    return {
      film: null,
      dateFormat: [], // 日期转换为需要的格式
      showCinemas: [], // 日期影院相关联的接口返回的数据
      cinemaIds: '', // 影院的ID
      dateCinames: []
      // listScroll: ''
    }
  },
  computed: {
    ...mapState('city', ['cityId']),
    ...mapState('dataListTimeStamp', ['showDateList'])
  },
  // watch: {
  //   dateCinames: function () {
  //     this.$nextTick(() => {
  //       this.listScroll = new BetterScroll('.wrapper', {
  //         scrollbar: {
  //           fade: true
  //         },
  //         click: true
  //       })
  //       this.listScroll.refresh()
  //     })
  //   }
  // },
  created () {},
  mounted () {
    console.log(this.$route)
    http({
      url: `/gateway?filmId=${this.$route.params.filmId}&k=7696343`,
      headers: {
        'X-Host': 'mall.film-ticket.film.info'
      }
    }).then((response) => {
      console.log(response.data.data.film)
      this.film = response.data.data.film
      this.getTimeStampCity(this.$route.params.filmId).then((response) => {
        this.dateFormat = this.modifyDate(this.showDateList)
        console.log(this.dateFormat)

        this.showCinemas = response
        // console.log(response);

        // this.changeIndex(0)
        this.cinemaIds = response[0].cinemaList.join(',')
        this.getShowData(this.cinemaIds)
      })
    })

    this.calcHeight = document.documentElement.clientHeight - 90 + 'px'
    // onresize 事件会在窗口或框架被调整大小时发生
    window.onresize = () => {
      console.log(document.documentElement.clientHeight)
      this.calcHeight = document.documentElement.clientHeight - 90 + 'px'
    }
  },
  methods: {
    ...mapActions('dataListTimeStamp', ['getTimeStampCity']),
    changeIndex (index) {
      console.log(index, 'index')
      this.cinemaIds = this.showCinemas[index].cinemaList.join(',')
      this.getShowData(this.cinemaIds)
    },
    handleFilm (cinemaId, filmId, timeStamp) {
      // `/cinema/${cinemaId}/film/${filmId}/${timeStamp}`
      this.$router.push({
        name: 'cinemafilm',
        params: {
          cinemaId,
          filmId,
          timeStamp,
          from: 'filmcinemas'
        }
      })
    },
    handleLeft () {
      this.$router.back()
    },
    handleRight () {
      this.$router.push('/cinemas/search')
    },
    getCinemaIds () {},
    getShowData (cinemaIds) {
      http({
        url: '/gateway?k=4460325',
        method: 'post',
        headers: {
          'X-Host': 'mall.film-ticket.cinema.batch-cinema'
        },
        data: {
          cityId: this.cityId,
          cinemaIds: cinemaIds
        }
      }).then((response) => {
        // console.log(response.data.data)
        this.dateCinames = response.data.data.cinemas

        // this.$nextTick(() => {
        //   this.listScroll = new BetterScroll('.wrapper', {
        //     scrollbar: {
        //       fade: true
        //     },
        //     click: true
        //   })
        // })
      })
    },
    modifyDate (arr) {
      console.log(arr)
      arr.sort((a, b) => {
        return b > a ? -1 : 1
      })
      // console.log(arr)
      // const newArr = arr.map(item => {
      //   const weekday = moment(item * 1000).format('dddd')
      //   const month = moment(item * 1000).format('M-D')
      //   return weekday.replace('星期', '周') + month.replace('-', '月') + '日'
      // })

      // const dateArr = []
      // for (let i = 0; i < arr.length; i++) {
      //   dateArr.push({
      //     timeStamp: arr[i],
      //     date: newArr[i]
      //   })
      // }
      // console.log(dateArr)
      return arr
    }
  }
}
</script>

<style scoped lang="scss">
.wrapper {
  overflow: hidden;
  position: relative; // 修正滚动条的位置
}

ul {
  background: #fff;
}

li {
  padding: 15px;
  border-bottom: 1px solid #f8f8f8;
  display: flex;
  align-items: center;
}

.left-text {
  max-width: 60px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.col-1 {
  flex: 1;
  padding-right: 15px;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  width: calc(100% - 70px);
  box-sizing: border-box;

  p {
    font-size: 15px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 100%;
  }

  span {
    color: #797d82;
    font-size: 12px;
    margin: 5px 0 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 100%;
  }
}

.col-2 {
  width: 70px;
  height: 45px;
  text-align: right;

  div {
    color: #ff5f16;
    font-size: 12px;

    span {
      font-size: 15px;
    }
  }

  p {
    color: #797d82;
    font-size: 12px;
    margin: 5px 0 0;
  }
}
</style>
